<template>
	<view>
		<view v-if="dataBuffer">
			<view class="item" v-for="(res, index) in siteList" :key="res.id">
				<u-icon name="close-circle-fill" :size="40" color="#ff5500" :index="res.id" @click="del(index, res.id)"></u-icon>
				<view class="top" @click="userAddress(index)">
					<view class="name">{{ res.username }}</view>
					<view class="phone">{{ res.userphone }}</view>
					<view class="tag"><text class="red" v-if="res.status == 1">默认</text></view>
				</view>
				<view class="bottom">
					{{ res.location + res.useraddress }}
					<u-icon name="edit-pen" :size="40" color="#999999" :index="res.id" @click="edit(index, res.id)"></u-icon>
				</view>
			</view>
		</view>
		<view v-else>暂无收货地址信息!</view>
		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>
				新建收货地址
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			siteList: [], //初始化地址数据
			dataBuffer: true
		};
	},
	onShow() {
		let uid = uni.getStorageSync('userInfo').uid;
		this.$http({
			url: 'addresslist',
			data: {
				uid
			}
		}).then(res => {
			if (res.list == null) {
				this.dataBuffer = false;
			} else {
				// console.log(res.list);
				this.siteList = res.list;

				//将收货地址存储到本地
				uni.setStorageSync('address', JSON.stringify(this.siteList));
			}
		});
	},
	methods: {
		// 跳转到新增地址页面
		toAddSite() {
			uni.navigateTo({
				url: './addSite'
			});
			uni.setStorageSync('isEdit', 2);
		},
		del(index, id) {
			//删除数据库中的数据 和本地数据
			this.$http({
				url: 'addressremove',
				method: 'post',
				data: { id }
			}).then(res => {
				this.$tip.tip('删除成功！');
				this.siteList.splice(index, 1);
			});
			if (this.siteList.length == 0) {
				this.dataBuffer = false;
			}
		},
		userAddress(index) {
			//如果从"我的"页面进入地址管理 点击返回"我的"
			if (uni.getStorageSync('AddressID') == 1) {
				uni.navigateBack({
					delta: 1
				});
			} else {
				//否则是从订单页面进入地址管理 点击返回订单页
				uni.redirectTo({
					url: '../order/order?address=' + JSON.stringify(this.siteList[index])
				});
			}
		},
		edit(index, id) {
			// console.log(index, id);
			uni.navigateTo({
				url: './addSite?address=' + JSON.stringify(this.siteList[index])
			});
			uni.setStorageSync('isEdit', 1);
		}
	}
};
</script>

<style lang="scss" scoped>
// 引入样式
@import '@/static/css/address.scss';
</style>
